﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using ArticleManager.DataClasses;

namespace ArticleManager.Forms
{
    public partial class frmManageKeysForm : Form
    {
        IList keywords;
        Controller _controller;

        private void PopulateKeywords()
        {
            lstKeys.Items.Clear();
            //keywords = _controller.Get.GetKeywords();
            foreach (Keyword keyword in keywords)
            {
                lstKeys.Items.Add(keyword);
            }
        }
        public frmManageKeysForm()
        {
            _controller = Controller.Instance;
            InitializeComponent();
            PopulateKeywords();

            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Keyword keyAdded = new Keyword();
            keyAdded.KeyText = txtKeyword.Text;
            //_controller.DataHelper.AddNewKeyword(ref keyAdded);
            //PopulateKeywords();
            lstKeys.Items.Add(keyAdded);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Keyword keyword = lstKeys.SelectedItem as Keyword;
            if (keyword != null)
            {
                lstKeys.Items.Remove(keyword);
                keyword.KeyText = txtKeyword.Text;
                //if (_controller.DataHelper.UpdateKeyword(keyword))
                //{
                    lstKeys.Items.Add(keyword);
                    txtKeyword.Text = "";
                //}
                //btnAdd.Enabled = false;
            } //end of if artice is not null.        
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Keyword keyword = lstKeys.SelectedItem as Keyword;
            if (keyword != null)
            {
                //if (_controller.DataHelper.DeleteKeyword(keyword))
                //{
                    lstKeys.Items.Remove(keyword);
                    txtKeyword.Text = "";
                //}
                //btnAdd.Enabled = false;
            } //end of if artice is not null.
        }

        private void lstKeys_SelectedIndexChanged(object sender, EventArgs e)
        {
            Keyword keyword = lstKeys.SelectedItem as Keyword;
            if (keyword != null)
            {
                txtKeyword.Text = keyword.KeyText;
                btnDelete.Enabled = true;
                btnUpdate.Enabled = true;
            } //end of if artice is not null.
        }
    }
}
